#include "formpriceselectwidget.h"
#include "ui_formpriceselectwidget.h"

#include <QListWidget>
#include <QDebug>

FormPriceSelectWidget::FormPriceSelectWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormPriceSelectWidget)
{
    ui->setupUi(this);

    connect (ui->btnBlack, SIGNAL(clicked()), this, SLOT(btnBlackClicked()));
    connect (ui->btnBlue, SIGNAL(clicked()), this, SLOT(btnBlueClicked()));
    connect (ui->btnGreen, SIGNAL(clicked()), this, SLOT(btnGreenClicked()));
    connect (ui->btnIndifferent, SIGNAL(clicked()), this, SLOT(btnNoneClicked()));
    connect (ui->btnRed, SIGNAL(clicked()), this, SLOT(btnRedClicked()));
    connect (ui->btnWhite, SIGNAL(clicked()), this, SLOT(btnWhiteClicked()));
    connect (ui->btnX, SIGNAL(clicked()), this, SLOT(btnXClicked()));

    connect (ui->chkSacrifise, SIGNAL(clicked(bool)), this, SLOT(chkSacrifiseClicked(bool)));
    connect (ui->chkTap, SIGNAL(clicked(bool)), this, SLOT(chkTapClicked(bool)));
    connect (ui->chkOnEnter, SIGNAL(clicked(bool)), this, SLOT(chkOnEnterClicked(bool)));

    txtPrice = ui->txtPrice;
    chkSacrifise = ui->chkSacrifise;
    chkTap = ui->chkTap;
    chkOnEnter = ui->chkOnEnter;
}

FormPriceSelectWidget::~FormPriceSelectWidget()
{
    delete ui;
}

void FormPriceSelectWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


void FormPriceSelectWidget::btnWhiteClicked()
{
    appendColorToPriceLine(WORD_COLOR_WHITE);
}

void FormPriceSelectWidget::btnBlackClicked()
{
    appendColorToPriceLine(WORD_COLOR_BLACK);
}

void FormPriceSelectWidget::btnBlueClicked()
{
    appendColorToPriceLine(WORD_COLOR_BLUE);
}

void FormPriceSelectWidget::btnRedClicked()
{
    appendColorToPriceLine(WORD_COLOR_RED);
}

void FormPriceSelectWidget::btnGreenClicked()
{
    appendColorToPriceLine(WORD_COLOR_GREEN);
}

void FormPriceSelectWidget::btnXClicked()
{
    QString condition = showSelectDialog(QStringList() << WORD_X_CARD_INHAND_DISCARD << WORD_X_CARD_INGRAVE << WORD_X_MANA_BLACK);
    QDomElement el = appendColorToPriceLine("X");
    el.setAttribute(WORD_ATTR_X_CONDITION, condition);
    el.setAttribute(WORD_ATTR_COUNT, 1);

    ui->txtPrice->setText(el.ownerDocument().toString());
}

QString FormPriceSelectWidget::showSelectDialog(QStringList items)
{
    QDialog *dialog = new QDialog;
    QVBoxLayout *layout = new QVBoxLayout();
    QListWidget *wdt = new QListWidget();
    layout->addWidget(wdt);

    connect(wdt, SIGNAL(doubleClicked(QModelIndex)), dialog, SLOT(close()));
    wdt->addItems(items);

    dialog->setLayout(layout);
    dialog->exec();
    return wdt->currentItem()->text();
}



void FormPriceSelectWidget::btnNoneClicked()
{
    appendColorToPriceLine("None");
}

void FormPriceSelectWidget::chkTapClicked(bool enabledChk)
{
    enableProperty(WORD_TAP, enabledChk);
}

void FormPriceSelectWidget::chkOnEnterClicked(bool enabledChk)
{
    enableProperty(WORD_ONENTER, enabledChk);
}

void FormPriceSelectWidget::chkSacrifiseClicked(bool enabledChk)
{    
    QDomElement el = enableProperty(WORD_SACRIFISE, enabledChk);
    if (enabledChk)
    {
        QString target = showSelectDialog(QStringList() << WORD_SACRIFISE_SELF << WORD_SACRIFISE_PERMANENT);
        if (target.compare(WORD_SACRIFISE_SELF) != 0)
        {
            el.setAttribute(WORD_ATTR_SACRIFISE_TARGET, target);
            ui->txtPrice->setText(el.ownerDocument().toString());
        }
    }

}

QDomElement FormPriceSelectWidget::appendColorToPriceLine(QString addedColor)
{
    QString curText = ui->txtPrice->text();

    QDomDocument *doc = new QDomDocument();
    doc->setContent(curText);
    QDomNodeList nodes = doc->elementsByTagName(WORD_PRICE);

    QDomElement rootElement;
    if (nodes.isEmpty())
    {
        rootElement= doc->createElement(WORD_PRICE);
        doc->appendChild(rootElement);
    }
    else
        rootElement = doc->documentElement();

    nodes = doc->elementsByTagName(addedColor);
    if (nodes.isEmpty())
    {
        QDomElement el = doc->createElement(addedColor);
        el.setAttribute(WORD_ATTR_COUNT, 1);
        rootElement.appendChild(el);
        ui->txtPrice->setText(doc->toString());
        return el;
    }
    QDomNode node = nodes.item(0);
    QDomElement el = node.toElement();
    el.setAttribute(WORD_ATTR_COUNT, el.attribute(WORD_ATTR_COUNT, 0).toInt() + 1);

    ui->txtPrice->setText(doc->toString());

    return el;
}

QDomElement FormPriceSelectWidget::enableProperty(QString enabledProperty, bool enabledChk)
{
    QString curText = ui->txtPrice->text();

    QDomDocument *doc = new QDomDocument();
    doc->setContent(curText);
    QDomNodeList nodes = doc->elementsByTagName(WORD_PRICE);

    QDomElement rootElement;
    if (nodes.isEmpty())
    {
        rootElement= doc->createElement(WORD_PRICE);
        doc->appendChild(rootElement);
    }
    else
        rootElement = doc->documentElement();

    QDomElement el;
    if (enabledChk)
    {
        el = doc->createElement(enabledProperty);
        rootElement.appendChild(el);
    }
    else
        rootElement.removeChild(doc->elementsByTagName(enabledProperty).item(0));
    ui->txtPrice->setText(doc->toString());
    return el;
}

void FormPriceSelectWidget::setCheckBoxesVisible(bool visible)
{
    ui->chkTap->setVisible(visible);
    ui->chkSacrifise->setVisible(visible);
    ui->chkOnEnter->setVisible(visible);

    ui->lblSacrifise->setVisible(visible);
    ui->lblTap->setVisible(visible);
    ui->lblOnEnter->setVisible(visible);
}
